package com.adou.second.day10;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class TreeSetInFileDemo {

	public static void main(String[] args) throws Exception {
		TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {

			@Override
			public int compare(Student stu1, Student stu2) {
				// 成绩从高到低
				int num = stu1.getSum() - stu2.getSum();
				
				int num2 = num == 0 ? stu1.getChinese() - stu2.getChinese():num;
				int num3 = num2 == 0 ? stu1.getChinese() - stu2.getChinese():num2;
				int num4 = num3 == 0 ? stu1.getChinese() - stu2.getChinese():num3;
				return num4;
			}
		});
		
		// 输入学生数据
		for (int i = 0; i < 2; i++) {
			
			Scanner sc = new Scanner(System.in);
			System.out.println("请录入第" + (i+1) + "个学生信息：");
			System.out.println("姓名：");
			String name = sc.nextLine();
			System.out.println("语文成绩：");
			int chinese = sc.nextInt();
			System.out.println("数学成绩：");
			int math = sc.nextInt();
			System.out.println("英语成绩：");
			int english = sc.nextInt();
			
			// 创建学生对象
			Student stu = new Student();
			stu.setName(name);
			stu.setChinese(chinese);
			stu.setMath(math);
			stu.setEnglish(english);
			
			// 添加学生对象
			ts.add(stu);
		}
		
		// 创建字符反冲输出流
		BufferedWriter bw = new BufferedWriter(new FileWriter("fos\\stu\\student.txt"));
		for (Student student : ts) {
			// 拼接
			StringBuffer sb = new StringBuffer();
			sb.append(student.getName()).append(" ")
			  .append(student.getChinese()).append(" ")
			  .append(student.getMath()).append(" ")
			  .append(student.getEnglish());
			
			bw.write(sb.toString());
			bw.newLine();
			bw.flush();
		}
		bw.close();
	}

}
